package cn.bugstack.trigger.http;
import cn.bugstack.api.IAiService;
import jakarta.annotation.Resource;
import jakarta.annotation.Resources;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

/**
 * @author ssh
 * @date 2025/7/27
 * @description
 */
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/v1/ollama/")
public class OllamaController implements IAiService{
    @Resource
    private OllamaChatClient chatClient;
    /**
     * 生成文本
     * http://localhost:8090/api/v1/ollama/generate?model=deepseek-r1:1.5b&message=hi
     * @return ChatResponse
     */
    @RequestMapping(value = "generate", method = RequestMethod.GET)
    @Override
    public ChatResponse generate(@RequestParam String model, @RequestParam String message) {
        return chatClient.call(new Prompt(message, OllamaOptions.create().withModel(model)));
    }
    /**
     * 流式生成文本
     * http://localhost:8090/api/v1/ollama/generate_stream?model=deepseek-r1:1.5b&message=你好
     * @return Flux<ChatResponse>
     */
    @RequestMapping(value = "generate_stream", method = RequestMethod.GET)
    @Override
    public Flux<ChatResponse> generateStream(@RequestParam String model, @RequestParam String message) {
        return chatClient.stream(new Prompt(message, OllamaOptions.create().withModel(model)));
    }
}
